
#define     DATAFLASH_CS_BIT       PB4                     // DataFlash CS strobe
#define     DATAFLASH_CS_PORT      PORTB                   // DataFlash CS strobe
#define     DATAFLASH_CS_PORT_DIR  DDRB                    // DataFlash CS strobe


void AT45DB011_init(void)
{
DATAFLASH_CS_PORT|= 1<<DATAFLASH_CS_BIT;                               // set DateFlash CS line as output
DATAFLASH_CS_PORT_DIR|= 1<<DATAFLASH_CS_BIT;                           // disable CS
}


// *****************************************
// * Write data (one byte) to DataFlash buffer
// *****************************************
void DATAFLASH_write_mem_buf(int adres,char data)
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x84;
    spi_wait();
    SPDR = 0;
    spi_wait();
    SPDR = (char) (adres>>8);
    spi_wait();
    SPDR = (char) adres;
    spi_wait();
    SPDR = data;
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
}

// *****************************************
// * Read data (one byte) from DataFlash buffer
// *****************************************
char DATAFLASH_read_mem_buf(int adres)
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x54;
    spi_wait();
    SPDR = 0;
    spi_wait();
    SPDR = (char) (adres>>8);
    spi_wait();
    SPDR = (char) adres;
    spi_wait();
    SPDR = 0;
    spi_wait();
    SPDR = 0;
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
    return (SPDR);
}

// *****************************************
// * Read data (one byte) directly from DataFlash with-out buffer
// * page_address - Page address in DataFlash
// * byte address - Byte addres in page
// *****************************************
char DATAFLASH_read_mem_main(int page_address,int byte_address )
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x52;
    spi_wait();

    SPDR = page_address>>7;
    spi_wait();

    SPDR = (char) (page_address<<1) | (char) (byte_address>>8);
    spi_wait();

    SPDR = (char) byte_address;
    spi_wait();

    SPDR = 0;
    spi_wait();

    SPDR = 0;
    spi_wait();

    SPDR = 0;
    spi_wait();

    SPDR = 0;
    spi_wait();

    SPDR = 0;             //  Read byte
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
    return (SPDR);
}


// *****************************************
// * Read DataFlash page to buffer
// * page_address - Page address in DataFlash
// *****************************************
void DATAFLASH_read_mem_page_to_buffer(unsigned int page_address)
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x53;
    spi_wait();

    SPDR = page_address>>7;
    spi_wait();


    SPDR = (char) (page_address<<1);
    spi_wait();

    SPDR = 0;
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
 }


// *****************************************
// * Write buffer to DataFlash page
// * page_address - Page address in DataFlash
// *****************************************
void DATAFLASH_write_mem_buffer_to_page(unsigned int page_address)
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x83;
    spi_wait();

    SPDR = page_address>>7;
    spi_wait();

    SPDR = (char) (page_address<<1);
    spi_wait();

    SPDR = 0;
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
  }


// *****************************************
// * Read DataFlash status
// * if bit 7 is set DataFlash is ready
// * if bit 7 is clear DataFlash is busy
// *****************************************
unsigned char DATAFLASH_read_mem_stat(void)
{
    DATAFLASH_CS_PORT&=~(1<<DATAFLASH_CS_BIT);
    SPDR = 0x57;
    spi_wait();
    SPDR = 0;
    spi_wait();
    DATAFLASH_CS_PORT|=1<<DATAFLASH_CS_BIT;
    return SPDR;
}

